package com.zbs.framework.core.permission.action;


import com.zbs.framework.common.utils.ResultData;
import com.zbs.framework.common.utils.ResultUtil;
import com.zbs.framework.dao.permission.entity.Permission;
import com.zbs.framework.dao.permission.service.PermissionService;
import com.zbs.framework.dao.permission.service.model.ServicePermission;
import com.zbs.framework.dao.permission.view.PermissionBasicView;
import com.zbs.framework.swagger.annotations.ApiOperation;
import com.zbs.framework.swagger.annotations.ApiParam;
import com.zbs.framework.swagger.annotations.ApiTags;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Create by Zbs on 2018/11/23.
 */
@ApiTags(value = "权限管理", group = "UIM")
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    /**
     * 添加权限
     *
     * @param serviceName
     * @param name
     * @param description
     * @param extra
     * @return
     */
    @ApiOperation(value = "添加权限",response = PermissionBasicView.class)
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('sys::addPermission')")
    public ResultData<Permission> add(@ApiParam(value = "服务名", required = true) String serviceName,
                          @ApiParam(value = "权限名", required = true) String name,
                          @ApiParam(value = "描述") String description,
                          @ApiParam(value = "其他信息") String extra,
                          @ApiParam(value = "接口路径") String path) {
        Permission permission = permissionService.save(serviceName, name, description, extra, path,true);
        //返回数据
        return ResultUtil.ok(permission);
    }

    /**
     * 编辑权限
     *
     * @param id
     * @param serviceName
     * @param name
     * @param description
     * @param extra
     * @return
     */
    @ApiOperation("编辑权限")
    @PostMapping("/edit")
    @PreAuthorize("hasAuthority('sys::editPermission')")
    public ResultData<?> edit(@ApiParam(value = "权限ID", required = true) Long id,
                           @ApiParam(value = "服务名", required = true) String serviceName,
                           @ApiParam(value = "权限名", required = true) String name,
                           @ApiParam(value = "描述") String description,
                           @ApiParam(value = "其他信息") String extra,
                           @ApiParam(value = "接口路径") String path) {
        permissionService.edit(id, serviceName, name, description, extra,path);
        return ResultUtil.ok();
    }

    /**
     * 删除权限
     *
     * @param id
     * @return
     */
    @ApiOperation("删除权限")
    @PostMapping("/delete")
    @PreAuthorize("hasAuthority('sys::deletePermission')")
    public ResultData<?> delete(@ApiParam(value = "权限ID", required = true) Long id) {
        permissionService.delete(id);
        return ResultUtil.ok();
    }

    /**
     * 获取权限树
     * @param serviceName
     * @return
     */
    @ApiOperation("获取权限树")
    @PostMapping("/findServicePermissions.do")
    public ResultData<List<ServicePermission>> findServicePermissions(@ApiParam(value = "服务名") String serviceName) {
        List<ServicePermission> result = permissionService.findServicePermissions(serviceName);
        return ResultUtil.ok(result);
    }
}
